package com.niit.controller;


import com.niit.apis.JobFeignApi;
import com.niit.entity.Application;
import com.niit.entity.Job;
import com.niit.entity.User;
import com.niit.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController()
@RequestMapping("/api/application")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private JobFeignApi jobFeignApi;


    //添加申请（求职者发起）
    @PostMapping("/add")
    public void addApplication(@RequestBody Application application){
        System.out.println(application);
        // 检查是否重复申请
        boolean alreadyApplied = applicationService.existsByJobIdAndSeekerId(
                application.getJob().getId(),
                application.getSeeker().getId()
        );

        if (alreadyApplied) {
            throw new RuntimeException("您已经申请过该职位");
        }
        // 设置申请时间和默认状态
        application.setApplyTime(LocalDateTime.now());
        application.setStatus((byte) 0); // 0=待定
        applicationService.addApplication(application);
    }

    //更新申请（求职者修改）
    @PutMapping("/update")
    public void updateApplication(@RequestBody Application application){
        System.out.println(application);
        applicationService.updateApplication(application);
//        return Result.success();
    }

    //删除申请（求职者与公司管理员删除）
    @DeleteMapping("/delete/{id}")
    public void deleteApplication(@PathVariable("id") int id){
        applicationService.deleteApplication(id);
//        return Result.success();
    }

    //获取所有申请
    @GetMapping("/getAll")
    public List<Application> getApplications(){
        return applicationService.getApplications();
    }

    //获取某个申请
    @GetMapping("/get/{id}")
    public Application getApplicationById(@PathVariable("id") int id){
        return applicationService.getApplicationById(id);
    }

    //获取某个求职者的申请
    @PostMapping("/getBySeeker")
    public List<Application>  getSeekerApplications(@RequestBody User seeker){
        return applicationService.getSeekerApplications(seeker);
    }

    //获取某个职位的申请
    @GetMapping("/getByJob")
    public List<Application> getJobApplications(@RequestBody Job job) {
        return applicationService.getJobApplications(job);
    }
    //根据工作的发布者获取所有的职位申请
    @PostMapping("/getByCompany")
    public List<Application> getCompanyApplications(@RequestBody User user) {
        return applicationService.getCompanyApplications(user);
    }

    //获取某个职位的申请数量
    @GetMapping("/getCountByJob")
    public long getCountByJob(@RequestBody Job job) {
        return applicationService.getCountByJob(job);
    }

//    @GetMapping("/resume/download/{resumeHash}")
//    public ResponseEntity<Resource> downloadResume(
//            @PathVariable String resumeHash,
//            @RequestParam(required = false) String filename) {
//
//        // 伪代码：实际需根据您的存储实现
//        Resource resource = storageService.load(resumeHash);
//
//        return ResponseEntity.ok()
//                .header(HttpHeaders.CONTENT_DISPOSITION,
//                        "attachment; filename=\"" + (filename != null ? filename : "resume") + "\"")
//                .body(resource);
//    }
}
